package com.jial08.designpatterns.prototype.deep.seria;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * <p>类描述：序列化实现深复制，复制的对象也要实现序列化。</p>
 *
 * @author jl
 * @version v1.0.0.1。
 * @since JDK1.8。
 * <p>创建日期：2019/10/26 下午6:08。</p>
 */
public class Student2 implements Serializable {

	private static final long serialVersionUID = -9111812742820430930L;
	private int age;
	private Address2 address;
	private Date birthday;
	private Integer grade;
	private List<String> list;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Address2 getAddress() {
		return address;
	}

	public void setAddress(Address2 address) {
		this.address = address;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public Student2 deepClone() {
		Student2 student = null;
		try {
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(this);

			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
			ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
			student = (Student2) objectInputStream.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return student;
	}

	@Override
	public String toString() {
		return "Student{" +
				"age=" + age +
				", address=" + address.getAdd() +
				", birthday=" + birthday +
				", grade=" + grade +
				", list=" + list +
				'}';
	}
}
